module.exports = (sequelize, DataTypes) => {
    const EntityInstance = sequelize.define('EntityInstance', {
        id: {
            type: DataTypes.STRING,
            primaryKey: true,
            allowNull: false
        },
        text: {
            type: DataTypes.STRING(255),
            allowNull: false
        },
        document_id: {
            type: DataTypes.STRING(24),
            allowNull: false
        },
        entity_id: {
            type: DataTypes.STRING(255),
            allowNull: false
        },
        is_checked: {
            type: DataTypes.BOOLEAN,
            defaultValue: true
        }
    }, {
        tableName: 'entity_instances',
        timestamps: true, // 启用创建日期和更新日期
        createdAt: 'created_at',
        updatedAt: 'updated_at',
        underscored: true // 使用下划线命名法
    });

    EntityInstance.associate = (models) => {
        // 可以建立与实体类和文档的关联
        if (models.EntityClass) {
            EntityInstance.belongsTo(models.EntityClass, {
                foreignKey: 'entity_id',
                targetKey: 'id'
            });
        }

        if (models.Document) {
            EntityInstance.belongsTo(models.Document, {
                foreignKey: 'document_id',
                targetKey: 'id'
            });
        }
    };

    return EntityInstance;
}; 